package dao;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;

import dominio.RemitoTransporte;

public class RemitoTransporteDAO {
	private static String query_buscarRemitoTransporte = "from RemitoTransporte r where r.id = (:id)";
	private static String query_listarRemitos = "from RemitoTransporte r where r.pendiente = true";

	private static RemitoTransporteDAO instancia = null;

	public static RemitoTransporteDAO getInstance() {
		if (instancia == null)
			instancia = new RemitoTransporteDAO();
		return instancia;
	}

	private RemitoTransporteDAO() {}

	public int guardarRemitoTransporte(RemitoTransporte rt) {
		Session s = HibernateDAO.getInstance().getSession();
		s.beginTransaction();
		s.saveOrUpdate(rt);
		s.getTransaction().commit();
		return rt.getId();
	}

	public RemitoTransporte buscarRemitoTransporte(int id) {
		Session s = HibernateDAO.getInstance().getSession();
		Query query = s.createQuery(query_buscarRemitoTransporte);
		query.setParameter("id", id);
		query.setMaxResults(1);
		return (RemitoTransporte) query.uniqueResult();
	}
	
	@SuppressWarnings("unchecked")
	public List<RemitoTransporte> listarRemitosPendientes() {
		Session s = HibernateDAO.getInstance().getSession();
		return s.createQuery(query_listarRemitos).list();
	}
}